package jmine.tec.report.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import jmine.tec.report.DataRow;
import jmine.tec.report.cell.DataCell;
import jmine.tec.report.cell.impl.StringDataCell;

/**
 * Implementação de um DataRow simples que carrega dados serializáveis com nenhuma ligação a objetos do Modelo etc.
 * 
 * @author julien �ltima modificação: 16/02/2007 Julien
 */
public class SimpleDataRow extends AbstractDataRow implements Serializable {

    private static final long serialVersionUID = 3827772145778790298L;

    private final String name;

    private final DataCell nameDataCell;

    /**
     * C'tor.
     * 
     * @param name String O nome da entidade representada pelo DataRow.
     */
    public SimpleDataRow(final String name) {
        this.name = name;
        this.nameDataCell = new StringDataCell(name);
    }

    /**
     * C'tor
     * 
     * @param nameDataCell the {@link DataCell} to be used as this column's name
     */
    public SimpleDataRow(final DataCell nameDataCell) {
        this.nameDataCell = nameDataCell;
        this.name = nameDataCell.toString();
    }

    /**
     * C'tor
     * 
     * @param nameDataCell2 the {@link DataCell}
     * @param cells the {@link List}
     * @param childRows the child rows
     */
    protected SimpleDataRow(final DataCell nameDataCell2, final List<DataCell> cells, final List<DataRow> childRows) {
        super(cells, childRows);
        this.nameDataCell = nameDataCell2;
        this.name = nameDataCell2.toString();
    }

    /**
     * O nome da entidade representada por este DataRow.
     * 
     * @return String
     */
    public final String getName() {
        return this.name;
    }

    /**
     * @see br.com.maps.fof.report.DataRow#getNameDataCell()
     * @return {@link DataCell}
     */
    public final DataCell getNameDataCell() {
        return this.nameDataCell;
    }

    /**
     * @return int
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = super.hashCode();
        result = prime * result + ((this.name == null) ? 0 : this.name.hashCode());
        return result;
    }

    /**
     * @param obj {@link Object}
     * @return boolean
     */
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (!super.equals(obj)) {
            return false;
        }
        if (this.getClass() != obj.getClass()) {
            return false;
        }
        final SimpleDataRow other = (SimpleDataRow) obj;
        if (this.name == null) {
            if (other.name != null) {
                return false;
            }
        } else if (!this.name.equals(other.name)) {
            return false;
        }
        return true;
    }

    /**
     * Ordena os DataRows filhos deste DataRow usando o Comparator especificado.
     * 
     * @param comparator Comparator
     * @return a copy of this {@link DataRow} that has its child rows sorted
     */
    public final DataRow sort(final Comparator<? super DataRow> comparator) {
        List<DataRow> children = new ArrayList<DataRow>(this.getChildRows().size());
        for (DataRow row : this.getChildRows()) {
            children.add(row.sort(comparator));
        }
        Collections.sort(children, comparator);
        return new SimpleDataRow(this.getNameDataCell(), this.getCells(), this.getChildRows());
    }

}
